//
//  AddrSearchResultsViewController.m
//  XPhoto
//
//  Created by baboy on 2019/6/24.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "AddrSearchResultsViewController.h"
#import "BKTableView.h"
#import "UIScrollView+x.h"

@interface AddrSearchResultsViewController ()

@end

@implementation AddrSearchResultsViewController

- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]) {
        self.page = 1;
    }
    return self;
}
- (void)setKeyword:(NSString *)keyword{
    if ( (_keyword == nil || _keyword.length==0) && (keyword == nil || keyword.length == 0)) {
        return;
    }
    if ([keyword isEqualToString:_keyword]) {
        return;
    }
    _keyword = keyword;
    self.page = 1;    
    [self search];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.definesPresentationContext = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView.backgroundColor = [UIColor blueColor];
    // Do any additional setup after loading the view.
}
#pragma tableview delegate
- (void)search{
    CLLocationCoordinate2D coordinate = self.location.coordinate;
    NSString *loc = [NSString stringWithFormat:@"%f,%f",coordinate.longitude, coordinate.latitude];
    [BKPlace search:self.keyword
       withLocation:loc
             radius:1000
               page:self.page
           callback:^(id task, id places, NSError * error) {
        DLOG(@"error:%@",error);
        if (self.page == 1) {
           self.places = [NSMutableArray array];
        }
        if (places) {
           [self.places addObjectsFromArray:places];
           [self.tableView reloadData];
           if (self.places.count>=20) {
               self.tableView.loadMore = YES;
           }
        }
        [self.tableView stopRefreshing];
        [self.tableView stopLoading];
    }];
    
    //    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    //        DLOG(@"%@",placemarks);
    //    }];
}

#pragma tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger n = self.places.count;
    return n;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CELL_REUSE_ID = @"ADDR_TABLE_CELL_ID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_REUSE_ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CELL_REUSE_ID];
    }
    UIView *accessoryView = nil;
    cell.accessoryView = accessoryView;
    BKPlace *place = [self.places objectAtIndex:indexPath.row];
    cell.textLabel.text = place.name;
    cell.detailTextLabel.text = place.addr;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BKPlace *place = [self.places objectAtIndex:indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(addrSearchViewController:didSelectPlace:)]) {
        [self.delegate addrSearchViewController:self didSelectPlace:place];
    }
    
}
- (void)loadMore:(BKTableView *)tableView{
    self.page += 1;
    [self search];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
